home *** CD-ROM | disk | FTP | other *** search
- ; Example 4: 3N+1
-
- ; This file doesn't illustrate anything in particular about
- ; the xComputer. It's just that I really like the 3N+1
- ; problem.
-
- ; Starting from any positive integer N, the "3N+1 sequence"
- ; for N is computed as follows: If N is 1, then stop; the
- ; sequence is complete. Otherwise, if N is even then divide
- ; N by 2. Otherwise (if N is odd), multiply N by three and
- ; add 1. The question is whether this sequence terminates
- ; for ALL starting values N. The answer is not known at
- ; this time.
-
- ; This program computes 3N+1 sequences for all values of
- ; N -- as long as you let it keep computing -- starting
- ; with 1. (The program itself is stored starting at
- ; location 900, so you can't really let the starting value
- ; become bigger than 900.) It counts the number of terms in
- ; each sequence and stores the answer for starting value N in
- ; memory location N. However, if in the course of the
- ; computation N becomes greater than the maximum allowed
- ; value (65535), I stop computing and store a -1 in the
- ; corresponding memory location to indicate an incomplete
- ; calculation.
-
- ; Run this with "Fastest" run speed and "Graphics" memory
- ; display and watch memory fill up!
-
-
- @PC 900 ; Make sure the PC is set to 900, the starting
- ; location of the program.
-
- 1024# 0 ; Clear out memory by storing 1024 zeros.
-
- @900 ; Start loading at locatin 900
-
- lod-c 1 ; Let num = 1. "Num" is the starting
- sto num ; value for the current sequence
-
- loop1: sto N ; "Loop1" computes one sequence; get the starting
- ; value from the AC, which now contains "num".
- lod-c 1 ; "Ct" keeps track of the number of terms
- sto ct ; in the sequence; start counting at 1.
-
- loop2: lod N ; "Loop2" computes one term in the sequence.
- dec ; Test if N=1 by subtracting 1 from it and
- jmz next ; testing if the answer is 0. If so, this
- ; sequence is complete; jump to "next" to
- ; get ready for the next sequence.
- and-c 1 ; Compute bitwise logical AND of 1 with N-1.
- jmz odd ; If the answer is 0, N is odd; jump to
- ; location "odd" to handle that case.
- lod N ; Otherwise, N is even; divide N by 2 by
- shr ; shifting it right, and putting the
- sto N ; result back into N. Then jump to
- jmp count ; "count" where this term in the sequence
- ; is counted.
- odd: lod N ; If N is odd, multiply it by 3 by adding it
- add N ; it to itself twice. Then add 1.
- jmf error ; If any of these additions produces a
- add N ; result greater than 65535, the FLAG
- jmf error ; register is set. This indicates an
- add-c 1 ; error: "Number too large for this
- jmf error ; computer". Jump to "error" if the
- sto N ; FLAG is set.
-
- count: lod ct ; Count this term in the sequence by
- inc ; incrementing the value of ct.
- sto ct
- jmp loop2 ; Return to start of "loop2" to do the
- ; next term in the sequence.
-
- next: lod ct ; The 3N+1 sequence for the current starting
- sto-i num ; value, num, is complete. Store the
- lod num ; number of terms in the sequence in the
- inc ; location given by the value of num,
- sto num ; then add 1 to num and jump back to
- jmp loop1 ; "loop1" to compute the next sequence.
-
- error: lod-c 0 ; A term in the current sequence has
- dec ; exceeded 65535. Store -1 (computed
- sto ct ; as zero minus one) in ct and jump to
- jmp next ; "next" to get ready for the next sequence.
-
- num: data ; starting value of sequence
- ct: data ; number of terms in the sequence
- N: data ; current value of N in sequence